<?php
/**
 * FeedbackAction.class.php
 * 留言模块
 *
 * @var string
 **/

class FeedbackAction extends PublicAction {

    protected $db,$fields;

    public function _initialize()
    {

        parent::_initialize();

        $fields = F($this->mod['Feedback'].'_Field');

        foreach($fields as $key => $res){
            if ($res['status']) {
                $res['setup'] = string2array($res['setup']);
                $this->fields[$key]=$res;
            }
        }
    }

    public function index($catid='')
    {        //获取路由规则
        $this->Urlrule = F('Urlrule');
        if(empty($catid))
            $catid = intval(I('id'));

        if ($catid) {
            $cat = $this->categorys[$catid];
            if(empty($module))
                $module = $cat['module'];
            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $this->assign('catid', $catid);
        }

        $this->display('Feedback:index');
    }

    /**
     * 录入
     *
     */
    public function insert()
    {
        $model = D('Feedback');
        $count = $model->where(' ip=\''.$_POST['ip'].'\' AND createtime > UNIX_TIMESTAMP()-86400')->count();
        if($count>5){
            $this->error('对不起，一天一人只能5条！请明天发送或直接给我们联系！');
            exit;
        }

        $fields = $this->fields;

        //验证码检测
        if($_POST['verify_status']){
            if((md5($_POST['verifyCode']) != $_SESSION['verify'])){
                $this->assign('script','javascript:history.go(-1);');
                $this->error(L('ERROR_VERIFY'));
            }
        }
        



        if(!empty($_SESSION['yzh_member'])){
            $_POST['userid'] = $_SESSION['yzh_member']['id'];
            $_POST['username'] = $_SESSION['yzh_member']['username'];
        }

        if (false === $model->create()) {
            $this->error($model->getError());
        }

        $id = $model->add();

        if ($id !==false) {

            $this->success(L('add_ok'));
        } else {
            $this->error(L('add_error').': '.$model->getDbError());
        }
    }
}